import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '../views/HomeView.vue'
import {ElMessage} from "element-plus";


const routes = [
    {
        path: '/',
        name: 'home',
        component: HomeView,
        //默认跳转路径：登陆界面
        redirect: '/admin',
        children: [
            {path: '/user', component: () => import('../views/user/UserView.vue')},
            {path: '/admin', component: () => import('../views/admin/AdminView.vue')},
            {path: '/announce', component: () => import('../views/announce/AnnounceView.vue')},
            {path: '/contentViolation', component: () => import('../views/index/ContentViolation.vue')},
            {path: '/stats', component: () => import('../views/stats/StatsView.vue')},
            {path: '/auditComment', component: () => import('../views/index/AuditCommentView.vue')},
        ]
    },
    {path: '/login', component: () => import('../views/LoginView.vue')}
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})

//添加全局路由守卫
router.beforeEach((to, from, next) => {
    const admin = localStorage.getItem("admin"); // 检查用户登录状态
    if (to.path !== '/login' && !admin) {
        ElMessage({
            message: "请先登录后再访问其他页面",
            type: "warning",
            duration: 3000, // 消息显示时间（毫秒）
        });
        next('/login'); // 未登录跳转到登录页面
    } else {
        next(); // 继续导航
    }
});
export default router
